package com.example.administrator.maizuofilm.activity_main;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.maizuofilm.BaseActivity;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.main.fragment.ActiveFragment;
import com.example.administrator.maizuofilm.card.fragment.CardFragment;
import com.example.administrator.maizuofilm.cienam.fragment.CienamFragment;
import com.example.administrator.maizuofilm.mine.fragment.MineFragment;
import com.example.administrator.maizuofilm.movie.fragment.MovieFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MainActivity extends BaseActivity implements MainContract.view {
    private static final int MOVIE = 0;
    private static final int CINEMA = 1;
    private static final int CARD = 2;
    private static final int ACTIVE = 3;
    private static final int MINE = 4;
    @InjectView(R.id.framelayout)
    FrameLayout framelayout;
    @InjectView(R.id.radio_movie)
    RadioButton radioMovie;
    @InjectView(R.id.radio_cinema)
    RadioButton radioCinema;
    @InjectView(R.id.radio_card)
    RadioButton radioCard;
    @InjectView(R.id.radio_active)
    RadioButton radioActive;
    @InjectView(R.id.radio_mine)
    RadioButton radioMine;
    @InjectView(R.id.radiogroup)
    RadioGroup radiogroup;
    private FragmentManager mManager;
    private MovieFragment mMovieFragment;
    private CienamFragment mCienamFragment;
    private CardFragment mCardFragment;
    private ActiveFragment mActiveFragment;
    private MineFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        mManager = getSupportFragmentManager();
        setSelection(R.id.radio_movie);
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_movie:
                        setSelection(checkedId);
                        break;
                    case R.id.radio_cinema:
                        setSelection(checkedId);
                        break;
                    case R.id.radio_card:
                        setSelection(checkedId);
                        break;
                    case R.id.radio_active:
                        setSelection(checkedId);
                        break;
                    case R.id.radio_mine:
                        setSelection(checkedId);
                        break;
                }
            }
        });
    }

    //设置选择
    private void setSelection(int index) {
        FragmentTransaction ft = mManager.beginTransaction();
        hideFragments(ft);
        switch (index) {
            case R.id.radio_movie:
                if (mMovieFragment == null) {
                    mMovieFragment = new MovieFragment();
                    mMovieFragment.setHasOptionsMenu(true);
                    ft.add(R.id.framelayout, mMovieFragment);
                } else {
                    ft.show(mMovieFragment);
                }
                break;
            case R.id.radio_cinema:
                if (mCienamFragment == null) {
                    mCienamFragment = new CienamFragment();
                    ft.add(R.id.framelayout, mCienamFragment);
                } else {
                    ft.show(mCienamFragment);
                }
                break;
            case R.id.radio_card:
                if (mCardFragment == null) {
                    mCardFragment = new CardFragment();
                    ft.add(R.id.framelayout, mCardFragment);
                } else {
                    ft.show(mCardFragment);
                }
                break;
            case R.id.radio_active:
                if (mActiveFragment == null) {
                    mActiveFragment = new ActiveFragment();
                    ft.add(R.id.framelayout, mActiveFragment);
                } else {
                    ft.show(mActiveFragment);
                }
                break;
            case R.id.radio_mine:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    ft.add(R.id.framelayout, mMineFragment);
                } else {
                    ft.show(mMineFragment);
                }
                break;

        }
        ft.commit();
    }

    //隐藏Fragment
    private void hideFragments(FragmentTransaction ft) {
        // TODO Auto-generated method stub
        if (mMovieFragment != null) {
            ft.hide(mMovieFragment);
        }
        if (mCienamFragment != null) {
            ft.hide(mCienamFragment);
        }
        if (mCardFragment != null) {
            ft.hide(mCardFragment);
        }
        if (mActiveFragment != null) {
            ft.hide(mActiveFragment);
        }
        if (mMineFragment != null) {
            ft.hide(mMineFragment);
        }
    }

}
